package com.llpy.controller;

import com.llpy.Service.CommentService;
import com.llpy.Service.ImgService;
import com.llpy.Service.UserService;
//import com.llpy.Utils.JwtTokenUtil;
import com.llpy.Utils.JwtTokenUtil;
import com.llpy.dto.CommentDto;
import com.llpy.mapper.UserMapper;
import com.llpy.pojo.Comment;
import com.llpy.pojo.User;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;


@RestController
public class UserController {
    private static String userImg;  //返回的图片id
    private final UserService userService;

//    private static User USER_NOW;
    @Autowired
    private ImgService imgService;
    @Autowired
    private CommentService commentService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }


    //注册用户
    @PostMapping("/register")
    public ResponseEntity<User> createUser(@RequestBody User user) {
        //设置默认图片
        user.setUserImg("646232b8886b0000d3003352");
        User savedUser = userService.createUser(user);
        return ResponseEntity.ok(savedUser);
    }

    //登录验证
    @PostMapping("/login")
    public ResponseEntity<?> login(@RequestBody User user) {

        return userService.getByName(user);
    }

    //删除用户
    @PostMapping("/deleteUser")
    public boolean DeleteUser(@RequestBody Map id){
        String id1 = (String) id.get("id");
        return userService.deleteUser(id1);
    }


    //更新头像
    @PostMapping("/update/userImg")
    public User updateUserImg(@RequestParam("file") MultipartFile file,HttpServletRequest request) throws Exception { //文件和用户id
        String id = request.getHeader("id"); //获得请求头的id
        userImg =  imgService.saveImage(file).toHexString();  //存储图片成功返回一个id

        return userService.updateImg(id,userImg); //将用户id和图片id传进方法
    }

    //更新用户信息
    @PostMapping("/update/userInfo")
    public User updateUserInfo(@RequestBody User user,HttpServletRequest request){  //用户数据和用户id
        String id = request.getHeader("id");
        return userService.updateInfo(user,id); //将用户id和用户数据传入方法
    }

    //保存评论
    @PostMapping("/saveComment")
    //获取到用户评论的内容，及前端传过来的id
    public Comment saveComment(@RequestBody CommentDto commentDto, HttpServletRequest request){
        String id = request.getHeader("id");  //获取前端的id
        return commentService.saveComment(id,commentDto.getTextarea());        //将id和评论内容传进方法
    }



}







